
/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2021
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
const authority = {
  //控制未登录操作
  setAuthSetting:function(){
    //禁用转发按扭
    wx.hideShareMenu();
  },
  //老师
  isTeacher:function(){
    return (this.getAuthType() == 2);
  },
  //学生
  isStudent:function(){
    return (this.getAuthType() == 1);
  },
  //登录状态
  checkAuth: function() {
    try {
      let userInfo = wx.getStorageSync('public_data')
      if (userInfo) {
        return true;
      }
    } catch (e) {
      console.error(e);
    }
    return false;
  },
  getAuthInfo: function() {
    try {
      let userInfo = wx.getStorageSync('public_data')
      if (userInfo) {
        return userInfo;
      }
    } catch (e) {
      console.error(e);
    }
    return false;
  },
  getAuthId: function() {
    let info = this.getAuthInfo();
    if (info) {
      return (info) ? info.userId : -1;
    }
  },
  getAuthToken: function() {
    let info = this.getAuthInfo();
    if (info) {
      return (info) ? info.userToken : '';
    }
  },
  getAuthType: function() {
    let info = this.getAuthInfo();
    if (info) {
      return (info) ? info.userType : '-1';
    }
  },
  getAuthNickname: function() {
    try {
      let info = this.getAuthInfo();
      if (info) {
        let nickname = wx.getStorageSync('sys_nickname');
        return nickname;
      }
    } catch (e) {
      console.error(e);
    }
    return '';
  },
  getAuthProtrait: function () {
    try {
      let info = this.getAuthInfo();
      if (info) {
        let portrait = wx.getStorageSync('sys_portrait');
        return portrait;
      }
    } catch (e) {
      console.error(e);
    }
    return '';
  },
  getAuthMobilephone: function () {
    try {
      let info = this.getAuthInfo();
      if (info) {
        let mobilephone = wx.getStorageSync('sys_mobilephone');
        return mobilephone;
      }
    } catch (e) {
      console.error(e);
    }
    return '';
  },
  clearUserCacheData: function(){
    wx.clearStorage();
  },
  //fwdata格式：{user_id:-1,user_token:'xxxxxx',nickname:'xxxx',avatar:'http://xxxx',mobile:'155xxxxaaaa'}
  setUserCacheData: function (fwdata) {
    var publicData = {};
    publicData.userId = fwdata.auth_id;
    publicData.userToken = fwdata.auth_token;
    publicData.userType = fwdata.auth_type;

    try {
      //接口请求公用参数
      wx.setStorageSync('public_data', publicData);
      // //本地缓存数据
      // wx.setStorageSync('sys_nickname', fwdata.nickname);
      // wx.setStorageSync('sys_portrait', fwdata.avatar);
      // wx.setStorageSync('sys_mobilephone', fwdata.mobile);
    } catch (e) {
      console.error(e);
    }
  },
  setUserNickname: function(nickname) {
    try {
      wx.setStorageSync('sys_nickname', nickname);
    } catch (e) {
      console.error(e);
    }
  },
  setUserProtrait: function(portrait) {
    try {
      wx.setStorageSync('sys_portrait', portrait);
    } catch (e) {
      console.error(e);
    }
  },
  setUserMobilephone: function(mobilephone) {
    try {
      wx.setStorageSync('sys_mobilephone', mobilephone);
    } catch (e) {
      console.error(e);
    }
  },
};

module.exports = authority;